package com.abcd.godating.web;

import java.security.Principal;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.abcd.godating.domain.Account;
import com.abcd.godating.service.AccountService;

@Controller
@RequestMapping("/account/*")
public class AccountController {

	@Inject
	private AccountService accountService;

	@RequestMapping(value = "/{userId}")
	public Account findAccountByUserId(@PathVariable long userId) {
		return accountService.findAccountByUserId(userId);
	}

	@RequestMapping(value = "/username/{username}")
	public Account findAccountByUsername(@PathVariable String username) {
		return accountService.findAccountByUsername(username);
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = "application/json")
	public @ResponseBody
	boolean updateAccount(Principal principal, @RequestBody Account account) {
		long userId = Long.valueOf(principal.getName());
		if (userId == account.getId()) {
			accountService.updateAccount(account);
			return true;
		}
		return false;
	}
}
